package com.online.onlinestudent.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.onlinestudent.dto.AjaxResult;
import com.online.onlinestudent.entity.Course;
import com.online.onlinestudent.entity.Record;
import com.online.onlinestudent.service.RecordService;
import com.online.onlinestudent.vo.RecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/records")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @GetMapping
    public AjaxResult getAllRecords() {
        List<Record> records = recordService.list();
        if (records == null || records.isEmpty()) {
            return AjaxResult.fail("未查询到结果！");
        }
        return AjaxResult.success(records);
    }

    @GetMapping("/reCoSt")
    public AjaxResult getAllRecordsVO() {
        List<RecordVO> recordVOs = recordService.getAllRecordsWithCourse();
        if (recordVOs == null || recordVOs.isEmpty()) {
            return AjaxResult.fail("未查询到结果！");
        }
        return AjaxResult.success(recordVOs);
    }
    /**
     * 根据id查询记录
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getRecordById(@PathVariable Long id) {
        Record record = recordService.getById(id);
        if (record != null) {
            return AjaxResult.success(record);
        } else {
            return AjaxResult.fail("不存在该记录！");
        }
    }

    /**
     *  根据学生id和课程id查询记录
     * @param student_id
     * @param course_id
     * @return
     */
    @GetMapping("/course")
    public AjaxResult getRecordByCourseId(@RequestParam Long student_id,@RequestParam Long course_id) {
        List<Record> record = recordService.getRecordsByStudentIDAndCourseId(student_id,course_id);
        if (record != null&&!record.isEmpty()) {
            return AjaxResult.success(record);
        } else {
            return AjaxResult.fail("不存在该记录！");
        }
    }

    /**
     * 根据学生id查询记录
     * @param id
     * @return
     */
    @GetMapping("/student/{id}")
    public AjaxResult getCourseByStudentId(@PathVariable Long id) {
        List<Course> courses = recordService.getRecordsByStudentId(id);
        if (courses != null&&!courses.isEmpty()) {
            return AjaxResult.success(courses);
        } else {
            return AjaxResult.fail("不存在该记录！");
        }
    }

    @PostMapping
    public AjaxResult saveRecord(@RequestBody Record record) {
        try {
            boolean saved = recordService.save(record);
            if (saved) {
                return AjaxResult.success("插入成功！");
            } else {
                return AjaxResult.fail("插入失败！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("插入失败：" + e.getMessage());
        }
    }

    @PutMapping("/saveUpdate")
    public AjaxResult saveOrUpdateRecord(@RequestParam Long student_id,@RequestParam Long course_id) {
        try {
            boolean updated = recordService.saveOrUpdateRecord(student_id,course_id);
            if (updated) {
                return AjaxResult.success("更新成功！");
            } else {
                return AjaxResult.fail("更新失败，请重新输入！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("更新失败：" + e.getMessage());
        }
    }
    @PutMapping("/{id}")
    public AjaxResult updateRecord(@PathVariable Long id, @RequestBody Record record) {
        record.setId(id);
        try {
            if(record.getProgress()<100)
            {
                record.setProgress(record.getProgress()+5);
            }
            LocalDateTime now = LocalDateTime.now();
            record.setUpdatedAt(now);
            boolean updated = recordService.updateById(record);
            if (updated) {
                return AjaxResult.success("更新成功！");
            } else {
                return AjaxResult.fail("更新失败，请重新输入！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("更新失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteRecordById(@PathVariable Long id) {
        try {
            boolean deleted = recordService.removeById(id);
            if (deleted) {
                return AjaxResult.success("删除成功！");
            } else {
                return AjaxResult.fail("删除失败，不存在该记录！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("删除失败：" + e.getMessage());
        }
    }

    @GetMapping("/page")
    public AjaxResult getRecordsByPage(@RequestParam int current, @RequestParam int size) {
        try {
            Page<Record> page = recordService.page(new Page<>(current, size));
            if (page == null || page.getRecords().isEmpty()) {
                return AjaxResult.fail("未查询到结果！");
            }
            return AjaxResult.success(page);
        } catch (Exception e) {
            return AjaxResult.fail("查询失败：" + e.getMessage());
        }
    }
}